﻿using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
namespace JEngine.Framework
{
    /// <summary>
    /// UI基类,这里会封装很多UI公用的特性
    /// </summary>
    public  class UIBase : MonoBehaviour
    {
        /// <summary>
        /// 定时器列表
        /// </summary>
        //private List<BaseTimerEvent> timerList = new List<BaseTimerEvent>();
        /// <summary>
        /// 获得一个定时器
        /// </summary>
        /// <param name="loop">循环次数，-1无限循环</param>
        /// <param name="delay">间隔时间（ms）</param>
        /// <param name="action">执行函数</param>
        // protected BaseTimerEvent NewTimer(int loop, long delay, UnityAction action)
        // {
        //     BaseTimerEvent timer = TimerManager.Instance.GetTimerEvent(loop, delay, action);
        //     timerList.Add(timer);
        //     return timer;
        // }
        protected  void Awake() {
            AddListener();
            InitComponent();
        }
        protected  void OnDestroy()
        {
            RemoveListener();
            ClearTimer();
        }

        /// <summary>
        /// 是否显示中
        /// </summary>
        /// <returns></returns>
        // public bool IsShow()
        // {
        //     return gameObject.layer != EnumGameLayer.UI_HIDE;
        // }

        /// <summary>
        /// 清空定时器
        /// </summary>
        protected void ClearTimer() {
            // for (int i = 0; i < timerList.Count; i++)
            // {
            //     if (timerList[i] != null)
            //     {
            //         timerList[i].TimerDisposed();
            //     }
            // }
            // timerList.Clear();
        }
        /// <summary>
        /// 添加监听，在Awake时调用
        /// </summary>
        protected virtual void AddListener() { }
        /// <summary>
        /// 移除监听，在OnDestroy时调用
        /// </summary>
        protected virtual void RemoveListener() { }

        /// <summary>
        /// 初始化组件，在Awake时调用
        /// </summary>
        protected virtual void InitComponent()
        {
            
        }

#if UNITY_EDITOR
        /// <summary>
        /// 一键引用，不区分大小写
        /// </summary>
        [ContextMenu("一键引用")]
        protected virtual void OnKeyReference()
        {
            //搜索公有、私有、保护类型
            BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
            Dictionary<string, Transform> transDic = GetTransforms();
            foreach (var field in this.GetType().GetFields(flags))//.Where(field => field.GetValue(this) == null))
            {
                //私有
                if (field.IsPrivate && !field.IsDefined(typeof(SerializeField), false))
                    continue;
                //保护
                if (!field.IsPrivate && !field.IsPublic && !field.IsDefined(typeof(SerializeField), false))
                    continue;
                Transform trans = null;
                if (transDic.TryGetValue(field.Name.ToLower(), out trans)) {
                    if(field.FieldType == typeof(GameObject))
                        field.SetValue(this, trans.gameObject);
                    else
                        field.SetValue(this, trans.GetComponent(field.FieldType));

                }
            }
        }



        protected Dictionary<string, Transform> GetTransforms()
        {
            Dictionary<string, Transform> transDic = new Dictionary<string, Transform>();
            Transform[] trans = this.transform.GetComponentsInChildren<Transform>(true);
            for (int i = 0; i < trans.Length; i++)
            {
                if (!transDic.ContainsKey(trans[i].name.ToLower())) {
                    transDic.Add(trans[i].name.ToLower(),trans[i]);
                }
            }
            return transDic;
        }
#endif

    }


}

